package servlet;
import it.polimi.swimv2.entities.User;
import it.polimi.swimv2.exception.UserDataError;
import it.polimi.swimv2.exception.UserDataException;
import it.polimi.swimv2.remote.RegistrationManagerRemote;
import it.polimi.swimv2.util.RemoteBindingName;

import java.io.IOException;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Servlet implementation class RegistrationServlet
 */
public class RegistrationServlet extends HttpServlet {
	private static final String ERROR_JSP = "error.jsp";
	private static final String CITY = "city";
	private static final String PHONE = "phone";
	private static final String EMAIL = "email";
	private static final String SURNAME = "surname";
	private static final String FIRSTNAME = "firstname";
	private static final String PASSWORD = "password";
	private static final String USERNAME = "username";
	private static final String REGISTRATION_ERROR = "registrationError";
	private static final String REG_SUMMARY_JSP = "registrationSummary.jsp";
	private static final String NEW_USER = "newUser";
	private static final String INDEX_JSP = "index.jsp";
	private static final String REGISTRATION_JSP = "registration.jsp";
	private static final String REGISTRATION_FORM = "registrationForm";
	private static final String ACT = "act";
	private static final long serialVersionUID = 1L;
	
   /**
    * @see HttpServlet#HttpServlet()
    */
   public RegistrationServlet() {
       super();
   }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		showRegistrationForm(request, response);
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void showRegistrationForm(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String action = request.getParameter(ACT);
		
		if(action.equals(REGISTRATION_FORM)){
			response.sendRedirect(REGISTRATION_JSP);
		}else{
			response.sendRedirect(INDEX_JSP);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//connection initialization
		processRegistration(request, response);	
	}
	/**
	 * manages the registration result
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void processRegistration(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {		
		try{
			RegistrationManagerRemote rem = retrieveRegistrationManagerRemote();
	
			User user = invokeCreateNewUser(rem, request); 
			
			if(user != null){
				setBlankValue(user);
				request.setAttribute(NEW_USER, user);
				RequestDispatcher dispatcher = request.getRequestDispatcher(REG_SUMMARY_JSP);
				dispatcher.forward(request, response); 
			}else{
				request.setAttribute(REGISTRATION_ERROR, UserDataError.VALUEERR);
				RequestDispatcher dispatcher = request.getRequestDispatcher(REGISTRATION_JSP);
				dispatcher.forward(request, response);
			}		
		}catch(NamingException e) {
			response.sendRedirect(ERROR_JSP);
		}catch(UserDataException e){
			request.setAttribute(REGISTRATION_ERROR, e.getErr());
			RequestDispatcher dispatcher = request.getRequestDispatcher(REGISTRATION_JSP);
			dispatcher.forward(request, response); 
		}
		
			}
	/**
	 * 
	 * @param user
	 */
	private void setBlankValue(User user) {
		if(user.getCity().equals("")){
			user.setCity("-");
		}
		if(user.getPhone().equals("")){
			user.setPhone("-");
		}		
	}
	/**
	 * manages the creation of new user
	 * @param rem
	 * @param request
	 * @return
	 * @throws UserDataException
	 */
	private User invokeCreateNewUser(RegistrationManagerRemote rem, HttpServletRequest request)
			throws UserDataException {
		
		String username = request.getParameter(USERNAME);
		String password = request.getParameter(PASSWORD);
		String firstname = request.getParameter(FIRSTNAME);
		String surname = request.getParameter(SURNAME);
		String email = request.getParameter(EMAIL);
		String phone = request.getParameter(PHONE);
		String city = request.getParameter(CITY);
		return ((User) rem.createNewUser(username, password, firstname, surname, email, phone, city));
	}
	/**
	 * retrieves the remote object
	 * @return
	 * @throws NamingException
	 */
	private RegistrationManagerRemote retrieveRegistrationManagerRemote() throws NamingException {	
		Context jndiContext = new InitialContext();
		Object ref = jndiContext.lookup(RemoteBindingName.REGISTRATION_MANAGER);
		RegistrationManagerRemote rem = (RegistrationManagerRemote) ref;
		return rem;
	}

}
